/*
 * Created on 24/10/2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package gminet.infra.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Transaction {

    private DBConnection dbConnection;

    public Transaction() throws TransactionException {

	super();
	try {
	    this.dbConnection = PoolManager.getInstance().getDBConnection();
	} catch (SQLException sqle) {
	    throw new TransactionException("Erro criando uma Transaction.", sqle);
	}
    }

    public Connection getConnection() throws TransactionException {

	if (isClosed()) {
	    throw new TransactionException("Não foi possivel obter a conexão.");
	}
	Connection conn = null;
	try {
	    conn = this.dbConnection.getConnection();
	} catch (SQLException sqle) {
	    throw new TransactionException("Erro obtendo a conexão. (" + sqle.getMessage() + ")");
	}
	return conn;
    }

    /*
         * (non-Javadoc)
         * 
         * @see gminet.infra.dao.Transaction#isClosed()
         */
    public boolean isClosed() {

	try {
	    return this.dbConnection == null || this.dbConnection.isTerminated();
	} catch (SQLException sqle) {
	    return true;
	}
    }

    /*
         * (non-Javadoc)
         * 
         * @see gminet.infra.dao.Transaction#close()
         */
    public void release() {

	if (!isClosed()) {
	    try {
		rollback();
		PoolManager.getInstance().releaseDBConnection(this.dbConnection);
	    } catch (SQLException sqle) {
		sqle.printStackTrace();
	    }
	}
    }

    /*
         * (non-Javadoc)
         * 
         * @see gminet.infra.dao.Transaction#commit()
         */
    public void commit() throws TransactionException {

	try {
	    getConnection().commit();
	} catch (SQLException sqle) {
	    throw new TransactionException("Erro efetuando o commit na transação.");
	}
    }

    /*
         * (non-Javadoc)
         * 
         * @see gminet.infra.dao.Transaction#rollback()
         */
    public void rollback() {

	try {
	    getConnection().rollback();
	} catch (TransactionException sqle) {
	    throw new RuntimeException("Erro efetuando o rollback na transação.");
	} catch (SQLException sqle) {
	    throw new RuntimeException("Erro efetuando o rollback na transação.");
	}
    }

    public PreparedStatement prepareStatement(String query) throws TransactionException {

	try {
	    return getConnection().prepareStatement(query);
	} catch (SQLException sqle) {
	    throw new TransactionException(sqle);
	}
    }

    public ResultSet executeQuery(String query) throws TransactionException {

	try {
	    PreparedStatement statement = getConnection().prepareStatement(query);
	    return statement.executeQuery();
	} catch (SQLException sqle) {
	    throw new TransactionException(sqle);
	}
    }

}
